package com.liwen.kefu.configuration;

import com.liwen.kefu.service.KeFuDataActionService;
import com.liwen.kefu.vo.BaseDataVo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.ZonedDateTime;
import java.util.Map;

/**
 * @author: Liwen
 * @createTime: 2024/11/10
 * @description:
 */

@Aspect
@Component
public class ControllerAspect {

    @Autowired
    private KeFuDataActionService keFuDataActionService;

    /**
     * 定义切点
     */
    @Pointcut(value = "execution(* com.liwen.kefu.controller.KeFuDataActionController.hiveImportDataToSql(..))")
    public void pointcut() {
    }

    /**
     * 请求接口前 查询userId
     *
     * @param joinPoint
     */
    @Before(value = "pointcut()")
    public void doBeforeMethod(JoinPoint joinPoint) {
        ZonedDateTime startZdt = ZonedDateTime.now();
        BaseDataVo baseDataVo = new BaseDataVo();
        Object[] obj = joinPoint.getArgs();
        for (Object o : obj) {
            baseDataVo = (BaseDataVo) o;
            break;
        }
        try {
            Map<String, Object> map = keFuDataActionService.queryUserId(baseDataVo.getMobileNo());
            baseDataVo.setUserId(map.get("id") + "");
            baseDataVo.setMobileNo(map.get("mobile_no") + "");
            baseDataVo.setStartZdt(startZdt);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
